package com.taru.order.dao;



import com.taru.order.entity.Articl;
import com.taru.order.entity.Group;
import com.taru.order.util.DbUtil;
import com.taru.order.util.StringUtil;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GroupDaoImpl  {

    ArtDaoImpl artDao=new ArtDaoImpl();

    public List<Group> getGroups()  {
        String sql="select * from `tb_group`";
        List<Map<String,Object>> list = DbUtil.query(sql);
        List<Group> groupList=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                Group group=new Group();
                group.setGroupId(StringUtil.valueOf(map.get("group_id")));
                group.setGroupName(StringUtil.valueOf(map.get("group_name")));
                group.setLogo(StringUtil.valueOf(map.get("logo")));
                group.setBrief(StringUtil.valueOf(map.get("brief")));
                groupList.add(group);
            }
        }
        DbUtil.close();
        return groupList;

    }


    public int addGroup(Group group)  {
        String sql="insert into `tb_group`(group_name,logo,brief) values(?,?,?)";
        int code=DbUtil.update(sql,group.getGroupName(),group.getLogo(),group.getBrief());
        return code;
    }


    public int modGroup(Group group)  {
        String sql="update `tb_group` set group_name=?,logo=?,brief=? where group_id=?";
        int code=DbUtil.update(sql,group.getGroupName(),group.getLogo(),group.getBrief(),group.getGroupId());
        return code;
    }
    //删除专题

    public int removeGroups(List<Integer> list) throws Exception {
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<list.size();i++){
            stringBuffer.append("\'");
            stringBuffer.append(list.get(i));
            stringBuffer.append("\',");
        }
        String sql="delete from `tb_group` where group_id in ("+stringBuffer.substring(0,stringBuffer.length()-1)+")";
        DbUtil.beginTransaction();
        int code=0;
        try {
            code=DbUtil.update(sql);
            DbUtil.commit();
        }catch (Exception e){
            e.printStackTrace();
            DbUtil.rollback();
            throw new Exception("DML异常");
        }
        DbUtil.close();
        return code;
    }
    //列表

    public Group getGroupById(String groupId){
        String sql="select * from `tb_articl_group` t1 left join `tb_group` t2 on t1.group_id=t2.group_id  where t1.group_id=?";
        List<Map<String, Object>> list = DbUtil.query(sql,groupId);
        Group group=null;
        if (list != null && list.size() > 0) {
            Map<String, Object> map=list.get(0);
            group=new Group();
            group.setGroupId(StringUtil.valueOf(map.get("group_id")));
            group.setGroupName(StringUtil.valueOf(map.get("group_name")));
            group.setLogo(StringUtil.valueOf(map.get("logo")));
            group.setBrief(StringUtil.valueOf(map.get("brief")));
            group.setArticls(listArticls(groupId));
        }
        return group;
    }

    public List<Articl> listArticls(String groupId){
        String sql="select * from tb_articl_group where group_id=?";
        List<Map<String, Object>> listArt = DbUtil.query(sql,groupId);
        List articlList=new ArrayList();
        if (listArt != null && listArt.size() > 0) {
            for(Map<String, Object> map:listArt){
                Articl articl=artDao.getArticlById(StringUtil.valueOf(map.get("articl_id")));
                articlList.add(articl);
            }
        }
        DbUtil.close();
        return articlList;
    }
}
